package com.arice.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.arice.SQLite.SQHelper;
import com.arice.beans.ExpensecodeInfo;
import com.arice.beans.IncomecodeInfo;

public class ExpenseCodeDao {

	private SQHelper sqHelper;
	private SQLiteDatabase db;
	
	public ExpenseCodeDao(Context context){
		sqHelper = new SQHelper(context);
	}
	public String getCodeNameByCodeId(int codeId){
		db = sqHelper.getReadableDatabase();
		Cursor c = db.rawQuery("select * from expensecode where codeId = "+codeId, null);
		String codeName = null;
		while(c.moveToNext()){
			codeName = c.getString(1);
		}
		
		c.close();
		db.close();
		return codeName;
	}
	public List<ExpensecodeInfo> getAllCodeName(){
		List<ExpensecodeInfo> list = new ArrayList<ExpensecodeInfo>();
		db = sqHelper.getReadableDatabase();
		Cursor c = db.rawQuery("select * from expensecode", null);
		while (c.moveToNext()) {
			int codeId = c.getInt(0);
			String codeName = c.getString(1);
			list.add(new ExpensecodeInfo(codeId, codeName));
		}
		c.close();
		db.close();
		return list;
	}
	public int getCodeIdByName(String codeName){
		db = sqHelper.getReadableDatabase();
		Cursor c  =db.rawQuery("select * from expensecode where codeName = '"+codeName+"'", null);
		int codeId = 0;
		while (c.moveToNext()) {
			codeId = c.getInt(0);
		}
		c.close();
		db.close();
		return codeId;
	}
	public int  insert(String codeName){
		db = sqHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("codeName", codeName);
		int id = (int) db.insert("expensecode", null, values);
		return id;
	}
}
